﻿using SlamDunk.CMS.DAL;
using SlamDunk.CMS.Model.DataModel;
using System;
using System.Collections.Generic;

namespace SlamDunk.CMS.BLL
{
    public class LinkBll
    {
        #region 01.实例化数据层
        /// <summary>
        /// 实例化数据层
        /// </summary>
        private readonly LinkDal CurrentDal = new LinkDal();
        #endregion

        #region 02.添加
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="link">构造好要添加的实体对象</param>
        /// <returns>添加完毕后的实体对象</returns>
        public Link Add(Link link)
        {
            return CurrentDal.Add(link);
        }
        #endregion

        #region 03.删除（物理删除）
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="id">要删除的实体对象Id</param>
        /// <returns>删除结果</returns>
        public bool PhysicalDelete(int id)
        {
            return CurrentDal.PhysicalDelete(id) > 0;
        }
        #endregion

        #region 05.修改
        /// <summary>
        /// 修改一个实体对象
        /// </summary>
        /// <param name="link">构造好的要修改的实体对象</param>
        /// <returns>修改结果</returns>
        public bool Update(Link link)
        {
            return CurrentDal.Update(link) > 0;
        }
        #endregion

        #region 06.根据主键获取单个实体对象
        /// <summary>
        /// 根据主键获取单个实体对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个实体对象</returns>
        public Link GetModel(int id)
        {
            return CurrentDal.GetModel(id);
        }
        #endregion

        #region 03.根据链接类别Id查询链接记录条数
        /// <summary>
        /// 根据链接类别Id查询链接记录条数
        /// </summary>
        /// <param name="categoryId">链接类别Id</param>
        /// <returns>符合条件的链接记录条数</returns>
        public int GetCount(int categoryId)
        {
            return this.CurrentDal.GetCount(categoryId);
        }
        #endregion

        #region 04.根据链接类别Id分页查询链接列表
        /// <summary>
        /// 根据链接类别Id分页查询链接列表
        /// </summary>
        /// <param name="start">起始行</param>
        /// <param name="end">结束行</param>
        /// <param name="categoryId">链接类别Id</param>
        /// <returns>链接列表</returns>
        public List<Link> GetModelList(int pageIndex, int pageSize, out int pageCount, out int rowCount, int categoryId)
        {
            //1.计算总记录条数
            rowCount = this.GetCount(categoryId);
            pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            //2.页索引处理
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
            //3.计算起始行与终止行
            int start = (pageIndex - 1) * pageSize + 1;
            int end = pageIndex * pageSize;
            return this.CurrentDal.GetModelList(start, end, categoryId);
        }
        #endregion

        #region 05.根据链接类别Id查询链接列表
        /// <summary>
        /// 根据链接类别Id分页查询链接列表
        /// </summary>
        /// <param name="categoryId">链接类别Id</param>
        /// <returns>链接列表</returns>
        public List<Link> GetModelList(int categoryId)
        {
            return this.CurrentDal.GetModelList(categoryId);
        }
        #endregion
    }
}
